﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
using System.Web.UI.WebControls;

namespace BLL
{
    public class BasePage_Admin : System.Web.UI.Page
    {
        private Model.User_Volunteer _CurrentUser;
        /// <summary>
        /// 当前登陆者
        /// </summary>
        public Model.User_Volunteer CurrentUser
        {
            get { return _CurrentUser; }
        }
        private Model.Role _CurrentRole;
        /// <summary>
        /// 当前登陆者的角色对象
        /// </summary>
        public Model.Role CurrentRole
        {
            get { return _CurrentRole; }
            set { _CurrentRole = value; }
        }
        protected override void OnLoad(EventArgs e)
        {
            CheckLogin();
            Fill_User();
            Fill_Role();
            base.OnLoad(e);
        }

        private void Fill_Role()
        {
            _CurrentRole = new BLL.Role().GetModel(_CurrentUser.RoleId);
        }

        private void Fill_User()
        {
            _CurrentUser = HttpContext.Current.Session["Volunteer"] as Model.User_Volunteer;
        }

        private void CheckLogin()
        {
            if (HttpContext.Current.Session["Volunteer"] == null)
            {
                HttpContext.Current.Response.Redirect("~/admin/NoLogin.aspx?url=" + HttpContext.Current.Request.Url.PathAndQuery);
            }
        }
        /// <summary>
        /// 错误时，退出。
        /// </summary>
        public void Quit_Error()
        {
            HttpContext.Current.Response.Redirect("~/admin/NoLogin.aspx");
        }
        
        #region QueryString参数相关
        public enum QueryStringType
        {
            Int,
            String
        }
        /// <summary>
        /// 获取int类型get参数（此方法会检查参数是否存在，如遇异常会Quit_Error）
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public int QueryString_Int(string str)
        {
            Check_QueryString(str, QueryStringType.Int);
            return Convert.ToInt32(Request.QueryString[str]);
        }
        /// <summary>
        /// get参数检查
        /// </summary>
        private void Check_QueryString(string argumentName, QueryStringType type)
        {
            if (HttpContext.Current.Request.QueryString[argumentName] == null)
            {
                Quit_Error();
            }
            string str = HttpContext.Current.Request.QueryString[argumentName];
            try
            {
                switch (type)
                {
                    case QueryStringType.Int:
                        Int32.Parse(str);
                        break;
                }
            }
            catch (Exception)
            {
                Quit_Error();
            }
        } 
        #endregion


        #region GetWhere
        public string GetWhere_List(string value, string fieldName)
        {
            if (value != "-1")
            {
                return " and " + fieldName + " = '" + value + "'";
            }
            return string.Empty;
        }
        public string GetWhere_List(int value, string fieldName)
        {
            if (value != -1)
            {
                return " and " + fieldName + " = " + value + "";
            }
            return string.Empty;
        }
        public string GetWhere_Str_Equal(string str, string fieldName)
        {
            if (!string.IsNullOrEmpty(str))
            {
                return " and " + fieldName + " = '" + str + "'";
            }
            return string.Empty;
        }
        public string GetWhere_Str_Like(string str, string fieldName)
        {
            if (!string.IsNullOrEmpty(str))
            {
                return " and " + fieldName + " like '%" + str + "%'";
            }
            return string.Empty;
        }

        public string GetWhere_Date(string fromDate, string toDate, string fieldName)
        {
            string where = string.Empty;
            if (!string.IsNullOrEmpty(fromDate) && !string.IsNullOrEmpty(toDate))
            {
                where += " and (" + fieldName + " Between '" + Convert.ToDateTime(fromDate + " 00:00:00").ToString() + "' and '" + Convert.ToDateTime(toDate + " 23:59:59").ToString() + "')";
            }
            else
            {
                if (!string.IsNullOrEmpty(fromDate))
                {
                    where += " and " + fieldName + " >= '" + Convert.ToDateTime(fromDate + " 00:00:00").ToString() + "'";
                }
                if (!string.IsNullOrEmpty(toDate))
                {
                    where += " and " + fieldName + " <= '" + Convert.ToDateTime(toDate + " 23:59:59").ToString() + "'";
                }
            }
            return where;
        } 
        #endregion

        #region Check
        public void Check_NotEmpty(string str, string msg)
        {
            if (string.IsNullOrEmpty(str.Trim()))
            {
                throw new Exception(msg);
            }
        }
        public void Check_NotEmpty(TextBox txt,string msg)
        {
            if (string.IsNullOrEmpty(txt.Text.Trim()))
            {
                throw new Exception(msg);
            }
        }
        public void Check_NotEmpty(DropDownList ddl, string msg)
        {
            if (ddl.SelectedValue == "-1")
            {
                throw new Exception(msg);
            }
        }
        public bool Check_Date(string fromDate, string toDate)
        {
            DateTime fromTime = DateTime.Now;
            DateTime toTime = DateTime.Now;
            try
            {
                if (!string.IsNullOrEmpty(fromDate))
                {
                    fromTime = DateTime.Parse(fromDate);
                }
            }
            catch (Exception)
            {
                Page.ClientScript.RegisterClientScriptBlock(GetType(), "", "alert(\"请输入正确的起始日期！\")", true);
                return false;
            }
            try
            {
                if (!string.IsNullOrEmpty(toDate))
                {
                    toTime = DateTime.Parse(toDate);
                }
            }
            catch (Exception)
            {
                Page.ClientScript.RegisterClientScriptBlock(GetType(), "", "alert(\"请输入正确的截止日期！\")", true);
                return false;
            }
            if (!string.IsNullOrEmpty(fromDate) && !string.IsNullOrEmpty(toDate))
            {
                if (toTime < fromTime)
                {
                    Page.ClientScript.RegisterClientScriptBlock(GetType(), "", "alert(\"起始日期不能晚于截止日期！\")", true);
                    return false;
                }
            }
            return true;
        } 
        #endregion

        public void Alert(string msg)
        {
            Page.ClientScript.RegisterClientScriptBlock(GetType(), "", "alert(\"" + msg + "\")", true);
        }
        public void Alert_Redirect(string msg, string url)
        {
            Page.ClientScript.RegisterClientScriptBlock(GetType(), "", "alert(\"" + msg + "\");window.location.href=\"" + url + "\";", true);
        }
    }
}
